home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / areuh.tar / areuh / cpy / copy.h < prev   
C/C++ Source or Header  |  1990-10-10  |  5KB  |  225 lines

  1. /*
  2.  * Authors :
  3.  *   Pierre DAVID (pda@masi.ibp.fr or pda@frunip62.bitnet)
  4.  *   Janick TAILLANDIER
  5.  *
  6.  * This program can be freely used or distributed as long as this
  7.  * note is kept.
  8.  *
  9.  * This program is provided "as is".
  10.  */
  11.  
  12. #include <stdio.h>
  13.  
  14. long int con () ;
  15.  
  16. typedef unsigned char uchar ;
  17.  
  18. typedef unsigned long int  int32 ;
  19. typedef unsigned short int int16 ;
  20.  
  21. #define ERRUSA           1        /* usage */
  22. #define ERROPN           2        /* cannot open */
  23. #define ERRNAL           3        /* not a Lex */
  24. #define ERRCLO           4        /* cannot close */
  25. #define ERRRD           5        /* error reading */
  26. #define ERRWRT           6        /* error writing */
  27. #define ERRLEN           7        /* bad REL(5) FiLeNd field */
  28. #define ERRINT           8        /* interface error */
  29.  
  30. uchar    *file ;
  31.  
  32. uchar    name [10] ;
  33. FILE    *fp ;
  34. int16    ftype ;
  35. int32    lnib, lbyte, lsect ;
  36.  
  37. uchar    *pgm ;                /* name of current program */
  38.  
  39. main (argc, argv)
  40. int argc ;
  41. char *argv[] ;
  42. {
  43.     long int magic ;
  44.     int32 l ;
  45.     uchar tab [4] ;
  46.     int i, j, m ;
  47.     int mod ;
  48.  
  49.     pgm = (uchar *) argv [0] ;        /* program name */
  50.  
  51.     switch (argc)
  52.     {
  53.     case 1 :
  54.         file = (uchar *) "lex" ;    /* default name */
  55.         break ;
  56.     case 2 :
  57.         file = (uchar *) argv [1] ;
  58.         break ;
  59.     default :
  60.         error (ERRUSA, "") ;
  61.         break ;
  62.     }
  63.  
  64.     fp = fopen (file, "rb") ;        /* binary file for MS-DOS machines */
  65.     if (fp == NULL)
  66.     error (ERROPN, file) ;
  67.  
  68. /* get Lex file header */
  69.  
  70.     fread ((char *) &magic, sizeof (long int), 1 , fp) ;
  71.     if (magic!=0x1b080100)
  72.     error (ERRNAL, file) ;        /* not a Lex file */
  73.  
  74.     for (i=0; i<8; i++)
  75.     name [i] = (uchar) con (2) ;
  76.     name [8] = name [9] = ' ' ;
  77.  
  78.     ftype = (int32) con (4) ;        /* file type */
  79.  
  80.     skip (12) ;             /* skip date and time */
  81.  
  82.     lnib = (int32)con (5) - (int32)5 ;    /* get length */
  83.     lsect = (lnib + 511) / 512 ;
  84.  
  85.     if (ferror (fp))            /* read error */
  86.     error (ERRRD, file) ;
  87.  
  88. /* initialize output channel */
  89.  
  90.     init () ;
  91.  
  92. /* header output */
  93.  
  94.     for (i=0; i<10; i++)        /* file name */
  95.     output ((uchar) name [i]) ;
  96.     output ((uchar) (ftype / 256)) ;    /* file type */
  97.     output ((uchar) (ftype % 256)) ;
  98.     for (i=1; i<=4; i++)        /* start sector */
  99.     output ((uchar) 0) ;
  100.     l = lsect ;
  101.     for (i=3; i>=0; i--)        /* length in sectors */
  102.     {
  103.     tab [i] = (uchar) l & 0xff ;
  104.     l >>= 8 ;
  105.     }
  106.     for (i=0; i<4; i++)
  107.     output (tab [i]) ;
  108.     for (i=0; i<6; i++)            /* date and time */
  109.     output ((uchar) 0) ;
  110.     output ((uchar) 0x80) ;        /* 80 */
  111.     output ((uchar) 0x01) ;        /* 01 */
  112.     l = lnib ;
  113.     for (i=0; i<4; i++)         /* length in nibbles */
  114.     {
  115.     output ((uchar) (l & (int32) 0xff)) ;
  116.     l >>= 8 ;
  117.     }
  118.  
  119. /* data output */
  120.  
  121.     lbyte = (lnib+1) >> 1 ;
  122.     mod = lbyte % 256 ;
  123.     for (i=1; i<=lsect; i++)
  124.     {
  125.     m = (i == lsect && mod != 0) ? min (256, mod) : 256 ;
  126.     for (j=0; j<m ; j++)
  127.         output ((uchar) con (2)) ;
  128.     }
  129.     for (j=m; j<256; j++)
  130.     output ((uchar) 0) ;
  131.  
  132. /* end */
  133.  
  134.     if (getc (fp) != EOF)        /* bad file len */
  135.     error (ERRLEN, file) ;
  136.     term () ;
  137.  
  138.     exit (0) ;
  139. }
  140.  
  141. long int con (n)
  142. int n ;
  143. {
  144.     int i ;
  145.     long int res ;
  146.     uchar buf [1024] ;
  147.     int first ;
  148.  
  149.     res = 0 ;
  150.     first = 1 ;
  151.     for (i=0; i<n; i++)
  152.     {
  153.     buf [i] = (uchar) getc (fp) ;
  154.     if (first && feof (fp))
  155.         error (ERRLEN, "") ;
  156.     first = 0 ;
  157.     }
  158.     if (ferror (fp))
  159.     error (ERRRD, file) ;
  160.     for (i=n-1; i>=0; i--)
  161.     res = (res << 4) | hex (buf [i]) ;
  162.     return res ;
  163. }
  164.  
  165. skip (n)
  166. int n ;
  167. {
  168.     uchar buf [1024] ;
  169.  
  170.     fread (buf, n, 1, fp) ;
  171.     if (ferror (fp))
  172.     error (ERRRD, file) ;
  173.     if (feof (fp))
  174.     error (ERRLEN, "") ;
  175. }
  176.  
  177. int min (a, b)
  178. int a, b ;
  179. {
  180.     return a <= b ? a : b ;
  181. }
  182.  
  183. int hex (c)
  184. uchar c ;
  185. {
  186.     return (c >= 'A' && c <= 'F') ? c - 'A' + 10 : c - '0' ;
  187. }
  188.  
  189. error (code, str)
  190. int code ;
  191. uchar *str ;
  192. {
  193.     switch (code)
  194.     {
  195.     case ERRUSA :        /* usage */
  196.         fprintf (stderr, "usage: %s [file]\n", pgm) ;
  197.         break ;
  198.     case ERROPN :        /* cannot open */
  199.         fprintf (stderr, "%s: cannot open %s\n", pgm, str) ;
  200.         break ;
  201.     case ERRNAL :        /* not a Lex */
  202.         fprintf (stderr, "%s: %s is not a lex file\n", pgm, str) ;
  203.         break ;
  204.     case ERRCLO :        /* cannot close */
  205.         fprintf (stderr, "%s: cannot close %s\n", pgm, str) ;
  206.         break ;
  207.     case ERRRD  :        /* error reading */
  208.         fprintf (stderr, "%s: error reading %s\n", pgm, str) ;
  209.         break ;
  210.     case ERRWRT :        /* error writing */
  211.         fprintf (stderr, "%s: error writing %s\n", pgm, str) ;
  212.         break ;
  213.     case ERRLEN :        /* bad REL(5) FiLeNd field */
  214.         fprintf (stderr, "%s: bad REL(5) FiLeNd field\n", pgm) ;
  215.         break ;
  216.     case ERRINT :        /* interface error */
  217.         fprintf (stderr, "%s: interface error\n", pgm) ;
  218.         break ;
  219.     default :
  220.         fprintf (stderr, "%s: internal error\n", pgm) ;
  221.         break ;
  222.     }
  223.     exit (1) ;
  224. }
  225.